package edu.upc.dew.filter;

import edu.upc.dew.util.ServletFilterOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/**
 * Generic wrapper to HttpServletResponse. Used to update response data if needed.
 */
public class GenericResponseWrapper extends HttpServletResponseWrapper {
    private ByteArrayOutputStream output;
    private int contentLength;
    private String contentType;
    
    public GenericResponseWrapper(HttpServletResponse res) {
        super(res);
        output = new ByteArrayOutputStream();
    }

    @Override
    public ServletOutputStream getOutputStream() { 
        return new ServletFilterOutputStream(output); 
    }

    @Override
    public PrintWriter getWriter() { 
        return new PrintWriter(getOutputStream(),true); 
    } 
 
    @Override
    public void setContentLength(int length) {
        this.contentLength = length;
        super.setContentLength(length);
    } 
 
    public int getContentLength() {
        return contentLength;
    }
 
    @Override
    public void setContentType(String type) {
        this.contentType = type;
        super.setContentType(type);
    }
 
    @Override
    public String getContentType() {
        return contentType;
    }
    
    public byte[] getData() {
        return output.toByteArray();
    }
}